package com.atguigu.search.controller;

import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.common.to.es.SkuEsModel;
import com.atguigu.common.utils.R;
import com.atguigu.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * description: ElasticSearchController
 * date: 2022/11/9 19:32
 * author: MR.孙
 */
@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticSearchController {

    @Autowired
    private ProductSaveService productSaveService;

    @PostMapping(value = "/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModel) {
        boolean b = false;
        try {

            b = productSaveService.productStatusUp(skuEsModel);

        } catch (Exception e) {
            log.error("ElasticSearchController商品上架失败", e);
            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMessage());
        }

        if (b) {

            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMessage());
        } else {

            return R.ok();

        }
    }

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }


}
